/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.template.archive;

import org.opu.vcforms.template.util.Loadable;
import org.opu.vcforms.template.util.ArchiveUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author yura
 */
public class AbstractArchiveTemplate implements Loadable {
    public static final String MAP_TMP = ".tpm.zip";
    public static final String OPU_VC_ARCHIVETEMPLATE = "opu_vc_archivetemplate";
    
    protected ArchiveUtil archiveUtil = new ArchiveUtil();
    private File id;

    @Override
    public final void load(InputStream inputStream) throws IOException {
        // read archive
        Map<String, byte[]> archive = archiveUtil.readArchive(inputStream);

        for (String name : archive.keySet()) {
            onLoad(name, archive.get(name));
        }

        afterDataLoaded();

        // create tmp file
        clear();
        id = File.createTempFile(OPU_VC_ARCHIVETEMPLATE, MAP_TMP);
        id.deleteOnExit();

        // write data to file
        archiveUtil.writeArchive(archive, new FileOutputStream(id));
    }

    @Override
    public final void store(OutputStream outputStream) throws IOException {
        // read stored tmp file
        Map<String, byte[]> archive = archiveUtil.readArchive(new FileInputStream(id));

        // get actual data
        Set<String> requere = forRequere();
        for (String name : new ArrayList<String>(archive.keySet())) {
            archive.put(name, onStore(name, archive.get(name)));
            requere.remove(name);
        }

        for (String name : requere) {
            archive.put(name, onStore(name, new byte[]{}));
        }

        // store data into output stream
        archiveUtil.writeArchive(archive, outputStream);
    }

    public void onLoad(String name, byte[] data) throws IOException{
        // By default do nothing
    }

    public byte[] onStore(String name, byte[] data) throws IOException{
        // By default no modifications with data
        return data;
    }

    public final Set<String> forRequere(){
        return new HashSet<String>(Arrays.asList(requred()));
    }

    public String[] requred(){
        // By default no requered names
        return new String[]{};
    }

    public void afterDataLoaded() throws IOException{
        // By default do nothing
    }

    protected final void clear(){
        if (id != null){
            id.delete();
        }
    }
}
